//
//  CityViewController.m
//  TodatPlay
//
//  Created by dllo on 16/9/19.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "CityViewController.h"
#import <sqlite3.h>
#import "sqliteData.h"
#import "City.h"
#import "HotCityTableViewCell.h"
#import "TAMacro.h"
#import "CityTableViewCell.h"
#import "NSString+ChineseTransForm.h"

#define kTableViewCell @"hotReuse"
@interface CityViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *cityTableView;
@property (nonatomic, strong) NSMutableArray *cityArr;
@property (nonatomic, strong) NSMutableArray *charaterArr;
@end

@implementation CityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _charaterArr = [NSMutableArray array];
    NSMutableArray *spellArr = [NSMutableArray array];
    // Do any additional setup after loading the view from its nib.
    sqliteData *sqliteManger = [sqliteData sharedManager];
    [sqliteManger openSQLite3];
    NSMutableArray *muArr = [NSMutableArray array];
    muArr = [sqliteManger selectAll];
    _cityArr = [NSMutableArray array];
    for (City *city in muArr) {
        if (![city.city_name containsString:@"盟"]) {
            if (![city.city_name containsString:@"自治州"]) {
                if (![city.city_name containsString:@"区"]) {
                    [_cityArr addObject:city];
                }
            }
        }
    }
    
    [self.cityTableView registerNib:[UINib nibWithNibName:@"HotCityTableViewCell" bundle:nil] forCellReuseIdentifier:kTableViewCell];
    [self.cityTableView registerNib:[UINib nibWithNibName:@"CityTableViewCell" bundle:nil] forCellReuseIdentifier:@"reuse"];
    
//     NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"postcode" ascending:YES];
//
//    [_cityArr sortUsingDescriptors:@[sortDescriptor]];
//    NSLog(@"%@", _cityArr)
    
    for (City *city in _cityArr) {
        NSString *pinyin = [NSString transform:city.city_name];
        [spellArr addObject:pinyin];
    }
    
    NSSortDescriptor *pinDescriptor = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES];
    [spellArr sortUsingDescriptors:@[pinDescriptor]];
//    NSLog(@"%@", spellArr);
    for (NSString *pinyinStr in spellArr) {
        NSString *firstCharacter = [pinyinStr substringWithRange:NSMakeRange(0, 1)];
        NSString *capital = firstCharacter.capitalizedString;
        [_charaterArr addObject:capital];
    }
    NSLog(@"%@", _charaterArr);

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 20;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0 | section == 1) {
        return 1;
    }else {
        return _cityArr.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        CityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
        cell.titleLabel.text = @"大连";
        return cell;
    } else if (indexPath.section == 1) {
        HotCityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCell];
        return cell;
    } else {
        CityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
        City *city = _cityArr[indexPath.row];
        cell.titleLabel.text = city.city_name;
        return cell;
    }
    
    
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 30, 40)];
    backView.backgroundColor = [UIColor colorWithRed:0.936 green:0.951 blue:0.982 alpha:1.000];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH - 40, 25)];
    label.font = [UIFont systemFontOfSize:13];
    [backView addSubview:label];
    if (section == 0) {
        label.text = @"当前定位城市";
    } else {
        
        label.text = @"热门城市";
    }
    return backView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 25;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        return 115;
    } else {
        return 30;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
